package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Setter
@Getter
public abstract class UserContext {
    //拿到字段
    private static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    private static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";
    //方法--拿当前的session
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }
    //拿到设置在session里的employee对象
    public static Employee getEmployee(){
        return (Employee) UserContext.getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }
    //拿到设置在session里的表达式list
    public static List<String> getExpressions(){
        return (List<String>) UserContext.getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }

    //将employee设置到session
    public static void setCurrentemp(Employee employee){
        UserContext.getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    //将expressions设置到session中
    public static void setExpressions(List<String> expressions){
        UserContext.getSession().setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }

}
